package com.nlscan.ncomgateway.core.database.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.nlscan.ncomgateway.core.database.model.DeviceEntity

/**
 * <pre>
 *  author: lin.qing
 *  time  : 2023/3/21
 *  desc  : DAO for [DeviceEntity] access
 * </pre>
 */
@Dao
interface DeviceDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevices(entities: List<DeviceEntity>): List<Long>

    @Query(value = "SELECT * FROM tb_device")
    fun getDeviceEntities(): LiveData<List<DeviceEntity>>

    @Query(value = "SELECT * FROM tb_device")
    fun getDeviceEntityList() : List<DeviceEntity>


    @Query(
        value = """
            SELECT * FROM tb_device WHERE address = :address
            """,
    )
    fun getDeviceEntity(address: String): DeviceEntity?

    /**
     * Updates [entities] in the db that match the primary key, and no-ops if they don't
     */
    @Update
    fun updateDevices(entities: List<DeviceEntity>)

//    @Upsert
//    suspend fun upsertDevices(entities:List<DeviceEntity>)

    @Query(
        value = "DELETE FROM tb_device WHERE address in (:addresses)"
    )
    fun deleteDevices(addresses: List<String>)
}